<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 第三方授权登录控制器
 * @author Calvin<hongbinfu@163.com>
 * qq weixin weibo 163 等等基于oauth2.0协议的第三方登录接口
 */
class Auth extends CI_Controller {
	
	public function __construct(){
		parent::__construct();

		//载入模型
		$this->load->library("form_validation");
		$this->config->load("form_validation");

		$services = array(
			'register_service',
			'login_service',
			'user_info_service',
			'user_verify_service'
		);
		$this->load->service($services);
	}
	
	public function session($provider = '') {
		$social = $this->config->item('social');
		if (empty($social)) redirect();

		$this->login_service->clear_oauth_info();
		$this->config->load('oauth2');
		$provider = strtolower($provider);
		$allowed_providers = $this->config->item('oauth2');
		if ( ! $provider OR ! isset($allowed_providers[$provider]))
		{
			redirect();
		}
		$auth_info = $this->login_service->get_oauth_info();
		if($auth_info && $auth_info['from']==$allowed_providers[$provider]['from']){
			redirect("auth/bind");
		}
		$this->load->library('Oauth2/oauth2',NULL,"oauth2");
		$provider = $this->oauth2->provider($provider, $allowed_providers[$provider]);
		$code = $this->input->get('code', TRUE);
		if ( ! $code)
		{
			try
			{
				$provider->authorize();
			}
			catch (OAuth2_Exception $e)
			{
				var_dump($e->getMessage());
			}
		}
		else
		{
			try
			{
				$token = $provider->access($code);
				$auth_info = $provider->get_user_info($token);
				$this->login_service->login_by_auth($auth_info);
				redirect("auth/bind");
			}
			catch (OAuth2_Exception $e)
			{
				var_dump($e->getMessage());
			}
		}
	}

	/**
	 *  绑定用户
	 */
	public function bind(){
		$auth_info = $this->login_service->get_oauth_info();
		if(empty($auth_info)){
			redirect();
		}
		if($auth_info['uid']!=0){
			$this->login_service->login_by_uid($auth_info['uid'],$auth_info['from']);
			redirect("/files");
		}
		$code = Common::uniqid();
		$this->template->assign("code",$code);
		$sms_code_len=$this->config->item("sms_verify_code_length","user_common");
		$this->template->assign("sms_code_len",$sms_code_len);
		$this->template->view('user/bind.tpl');
	}


	public function check_phone(){
		$auth_info = $this->login_service->get_oauth_info();
		if(empty($auth_info)){
			Common::ajax_return(1003);
		}
		//check_bind_by_phone
		$this->form_validation->set_rules('mobile','', 'required|valid_mobile_phone');
		if ($this->form_validation->run() == FALSE)
		{
			return Common::ajax_return(1003);
		}
		else {
			$mobile = $this->input->post("mobile",TRUE);
			$this->login_service->check_bind_by_phone($mobile,$auth_info['from']);
			Common::ajax_return($this->login_service);
		}
	}

	/**
	 *
	 * @return array
	 */
	public function send_sms(){
		$auth_info = $this->login_service->get_oauth_info();
		if(empty($auth_info)){
			Common::ajax_return(1003);
		}
		$this->form_validation->set_rules('mobile','', 'required|valid_mobile_phone');
		if ($this->form_validation->run() == FALSE)
		{
			return Common::ajax_return(1003);
		}
		else
		{
			$mobile = $this->input->post("mobile",TRUE);
			$check = $this->login_service->check_bind_by_phone($mobile,$auth_info['from']);
			if(!$check){
				Common::ajax_return($this->login_service);
			}
			$send = $this->user_verify_service->send_sms($mobile,User_verify_service::PROCESS_BIND_AUTH_USER);
			if($send === FALSE)
			{
				return Common::ajax_return($this->user_verify_service);
			}
			return Common::ajax_return(0);
		}
	}


	public function do_bind(){
		$auth_info = $this->login_service->get_oauth_info();
		if(empty($auth_info)){
			Common::ajax_return(1003);
		}
		if($auth_info['uid']==0){
			$mobile = $this->input->post("mobile",TRUE);
			$code = $this->input->post("code",TRUE);
			if(empty($mobile) || empty($code))
			{
				return Common::ajax_return(1003);
			}
			$check = $this->user_verify_service->check_sms_code($code,$mobile,User_verify_service::PROCESS_BIND_AUTH_USER);
			if($check===FALSE)
			{
				return Common::ajax_return($this->user_verify_service);
			}else{
				$check_phone = $this->register_service->check_phone_exists($mobile);
				if(!$check_phone){
					$password = Common::random_number(8);
					$add = $this->register_service->register_by_phone($mobile,$password,'','',array("image"=>$this->user_info_service->fetch_remote_avatar($auth_info['head_img']),"nickname"=>$auth_info['name']),TRUE);
					if(!$add){
						Common::ajax_return($this->register_service);
					}
					$uid = $this->register_service->get_register_uid();
				}else{
					$uid = $check_phone['id'];
					//$user_info = $this->user_info_service->get_user_info($uid);
					$update_data = array();
					/*if(empty($user_info['image'])){
						$update_data['image']=$auth_info['head_img'];
					}
					if(empty($user_info['nickname'])){
						$update_data['nickname']=$auth_info['name'];
					}*/
					if(!empty($update_data)){
						$this->user_info_service->update_user_info($uid,$update_data);
					}
				}
				$bind = $this->login_service->bind_auth_user($auth_info['id'],$uid,$auth_info['from']);
				if(!$bind){
					Common::ajax_return($this->login_service);
				}
			}
		}else{
			$uid = $auth_info['uid'];
		}
		$this->login_service->login_by_uid($uid,$auth_info['from']);
		Common::ajax_return(0,NULL,array("redirect"=>"/files"),FALSE);
	}


	
	
	
}